package com.whmnx.shop.ui.adapter;

import android.content.Context;
import android.view.View;

import com.whmnx.base.bean.ShoppingBean;
import com.whmnx.shop.R;

/**
 * Company 武汉麦诺软创
 * Created by lizhe on 2018/7/7.
 */

public class ShopCarAdapter extends BaseAdapter<ShoppingBean> {

    public OnEditCall onEditCall;
    private boolean isEdit;

    public void setOnEditCall(OnEditCall onEditCall) {
        this.onEditCall = onEditCall;
    }

    public ShopCarAdapter(Context context) {
        super(context);
    }

    public void allSelect(boolean isAll) {

        for (ShoppingBean shoppingBean : getDataSource()) {
            shoppingBean.setSelect(isAll);
        }
        notifyDataSetChanged();

        if (onEditCall != null) {
            onEditCall.heji();
        }
    }

    public void setEdit(boolean isEdit) {
        this.isEdit = isEdit;
        notifyDataSetChanged();
    }

    public void setSelect(int position, boolean isSelect) {
        getDataSource().get(position).setSelect(isSelect);
        notifyItemChanged(position);
    }

    @Override
    protected void bindDataToItemView(BaseViewHolder holder, ShoppingBean item, int position) {

        holder.setText(R.id.tv_goods_name, (String) item.getShopCart_ProductName());
        holder.setText(R.id.tv_guige_name, "规格: " +  item.getShopCart_SPECName());
        holder.setText(R.id.tv_moeny, "￥" + item.getShopCart_SPECMoney());
        holder.setText(R.id.tv_edit_count, item.getShopCart_Num() + "");

        if (item.isSelect()) {
            holder.setImageResource(R.id.iv_select, R.mipmap.check2);
        } else {
            holder.setImageResource(R.id.iv_select, R.mipmap.check1);
        }


        holder.setOnClickListener(R.id.iv_select, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.isSelect()) {
                    item.setSelect(false);
                    holder.setImageResource(R.id.iv_select, R.mipmap.check1);
                } else {
                    item.setSelect(true);
                    holder.setImageResource(R.id.iv_select, R.mipmap.check2);
                }

                if (onEditCall != null) {
                    onEditCall.isAll();
                    onEditCall.heji();
                }
            }
        });


    }

    @Override
    protected int getItemViewLayoutId(int position, ShoppingBean item) {
        return R.layout.item_shop2;
    }



    public interface OnEditCall {
        void collection(String goodsId);

        void updateSpec(int fposition, int position, String carId, String goodsId);

        void heji();

        void isAll();

        void sonSelect(int position);

        void updateData();

      //  void onReceive(ShopCarBean shopCarBean);
    }

}
